import {ChangeDetectorRef, Component, EventEmitter, Inject, Input, OnInit, Output, ViewChild} from '@angular/core';
import {SFSchema} from '@delon/form';
import {STColumn} from '@delon/abc';
import {BehaviorSubject, Observable, of} from 'rxjs';
import {NzModalService} from 'ng-zorro-antd';
import {BaseGridComponent, BaseService, Page} from '@sb/base';
import {OuOrgService} from '../../service/ou-org.service';
import {OuOrgSearchForm} from '../../entity/ou-org-search-form';
import {OuOrg, OuOrgType} from '../../entity/ou-org';

@Component({
    selector: 'ou-sub-org-tab',
    templateUrl: './ou-sub-org-tab.component.html',
    styleUrls: ['./ou-sub-org-tab.component.less'],
    providers: [{
        provide: BaseService,
        useClass: OuOrgService
    }]
})
export class OuSubOrgTabComponent implements OnInit {

    @Input()
    name = '下级机构';

    @Output() dataChange = new EventEmitter<any>();

    @Output() itemClickEvent = new EventEmitter<any>();

    @ViewChild('baseGrid', {static: true})
    baseGrid: BaseGridComponent;
    typeIcon = {
        root: 'dr:hy-root',
        ORG: 'dr:hy-module',
        DEPT: 'dr:hy-terminal',
        USER: 'dr:user_yw'
    };
    schema: SFSchema;
    columns: STColumn[];

    constructor(
        private modal: NzModalService,
        @Inject(ChangeDetectorRef) public readonly cd: ChangeDetectorRef) {
    }

    @Input()
    set baseParam(_baseParam: any) {
        if (_baseParam) {
            this.updateSchema(_baseParam.orgType);
            this.updateColumn(_baseParam.orgType);
            this.baseGrid.searchForm = {
                orgType: 'ORG',
                parentOrgId: _baseParam.parentOrgId
            } as Page;
            this.baseGrid.baseParam = {
                parentOrgId: _baseParam.parentOrgId,
                orgType: _baseParam.orgType
            };
            if (this.baseGrid.tableDataObservable) {
                this.baseGrid.reload();
            }
        }
    }

    ngOnInit() {
    }

    updateColumn(orgType: OuOrgType) {
        if (orgType === 'ORG') {
            this.columns = [
                {
                    title: '',
                    type: 'checkbox',
                    index: 'roleId',
                    width: '10%',
                    className: 'text-center word-wrap',
                }, {
                    title: '机构名称',
                    index: 'orgName',
                    width: '15%',
                    className: 'text-center word-wrap',
                }, {
                    title: '机构地址',
                    index: 'orgAddr',
                    width: '25%',
                    className: 'text-center word-wrap',
                }, {
                    title: '联系人',
                    index: 'linkMan',
                    width: '15%',
                    className: 'text-center word-wrap',
                }, {
                    title: '联系电话',
                    index: 'linkTel',
                    width: '15%',
                    className: 'text-center word-wrap',
                }
            ] as STColumn[];
        } else if (orgType === 'DEPT') {
            this.columns = [
                {
                    title: '',
                    type: 'checkbox',
                    index: 'roleId',
                    width: '10%',
                    className: 'text-center word-wrap',
                }, {
                    title: '部门名称',
                    index: 'orgName',
                    width: '70%',
                    className: 'text-center word-wrap',
                }
            ] as STColumn[];
        }
    }

    updateSchema(orgType: OuOrgType) {
        if (orgType === 'ORG') {
            this.schema = {
                properties: {
                    orgName: {
                        type: 'string',
                        title: '机构名称',
                    },
                    // managerUserId: {
                    //     type: 'string',
                    //     title: '机构管理员',
                    //     ui: {
                    //         widget: 'ou-user-select-alain'
                    //     }
                    // },
                    orgAddr: {
                        type: 'string',
                        title: '机构地址'
                    },
                    linkMan: {
                        type: 'string',
                        title: '联 系 人'
                    },
                    linkTel: {
                        type: 'string',
                        title: '联系电话'
                    },
                    email: {
                        type: 'string',
                        title: '电子邮箱'
                    },
                    webUrl: {
                        type: 'string',
                        title: '网站地址'
                    }
                },
                required: ['orgName']
            } as SFSchema;
        } else if (orgType === 'DEPT') {
            this.schema = {
                properties: {
                    orgName: {
                        type: 'string',
                        title: '部门名称',
                    }
                },
                required: ['orgName']
            } as SFSchema;
        }
    }

    onDataReload() {
        this.dataChange.emit();
    }

    beforeRemove(orgs: OuOrg[]): Observable<boolean> {
        const subject = new BehaviorSubject(false);
        for (let i = 0; i < orgs.length; i++) {
            const org = orgs[i];
            if (org.orgType === 'ORG') {
                this.modal.confirm({
                    nzTitle: '删除提示',
                    nzContent: '删除机构会同时删除机构下的所有部门，是否确定删除?',
                    nzOnOk: () => {
                        subject.next(true);
                        subject.complete();
                    }
                }, 'warning');
                break;
            }

            if (i === orgs.length - 1) {
                return of(true);
            }
        }
        return subject;
    }

    itemClick(org: OuOrg) {
        this.itemClickEvent.emit(org.orgId);
    }

}
